package com.itheima.controller;


import com.github.pagehelper.Page;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author wangchang
 * @since 2020-08-04
 */
@RestController
@RequestMapping("/user")
@CrossOrigin(allowCredentials="true", allowedHeaders="*", methods={RequestMethod.GET,RequestMethod.POST}, origins="*")
public class UserController {
    @Autowired
    private UserService userService;
    
    // 删除用户
    @PostMapping("/delete")
    public Result delete(@RequestBody User user) {
        String deleteResult = userService.deleteUser(user);
        if ("failed".equals(deleteResult)) {
            return new Result("error", "删除失败");
        }
        return new Result("success","删除成功");
    }

    
    // 编辑用户
    @PostMapping("/edit")
    public Result edit(@RequestBody User user) {
        String editResult = userService.editUser(user);
        if ("failed".equals(editResult)) {
            return new Result("error", "编辑失败");
        }
        return new Result("success","编辑成功");
    }

    // 获取用户列表
    @GetMapping("/getList")
    public Result getUserList() {
        Page<User> pageList = userService.getUserList();
        PageResult pageResult = new PageResult(pageList.getTotal(), pageList.getResult());
        return new Result("200", pageResult);
    }

    // 更改用户密码
    @PostMapping("/resetUserPassword")
    public Result resetUserPassword(@RequestBody User user) {
        String resetResult = userService.resetUserPassword(user);
        if ("failed".equals(resetResult)) {
            return new Result("error", "更改失败");
        }
        return new Result("success","更改成功");
    }
}

